var Transcript = {
    sortfunction: function(keya, keyb) {
        // suppose we get two course hashes
        a = Globals.courses[keya];
        b = Globals.courses[keyb];

        if (a.instance.year > b.instance.year) return -1;
        else if (a.instance.year < b.instance.year) return 1;
        else if (a.instance.term < b.instance.term) return -1;
        else if (a.instance.term > b.instance.term) return 1;
        else if (a.grade < b.grade) return -1;
        else return 1;
    },
    open: function() {
        /*var keys = $H(Globals.courses).keys();
        keys.sort(Transcript.sortfunction)
        var text = ""
        for (key in keys) {
            var term = Globals.courses[key].instance.term ? "WS " : "SS ";
            text += "\t<tr>\n"
            text += "\t\t<td>"+term+Globals.courses[key].instance.year"</td>\n"
            text += "\t\t<td>"+Globals.courses[key].name"</td>\n"
            text += "\t\t<td>"+Globals.courses[key].instance.key"</td>\n"
            text += "\t\t<td>"+Globals.courses[key].credits"</td>\n"
            text += "\t\t<td>"+Globals.courses[key].grade"</td>\n"			
            text += "\t</tr>\n"
        }
        */
        
        text = $H(Globals.courses).toJSON();
        $("postformq").value = text;
        $("postform").submit();

//        var printView = window.open("templates/transcript.php?data="+text,"Transcript of Records","width=600,height=400,scrollbars=1,resizable=1,toolbar=0,menubar=0")
        
    }
}

var PrintView = {
    sectionOpen: false,
    
    section: function(title) {
        PrintView.sectionOpen = !PrintView.sectionOpen;
        if (PrintView.sectionOpen)
            return "<div class='section'><h2>"+title+"</h2>\n";
    	else 
    	    return "</div>\n"
    },
    
    hr : function() {
        return "<span class='hr'>--------------------------------------------------</span><br />\n";
    },
    
    show: function() {
        var printView = window.open("","SPAM - Summary","width=600,height=400,scrollbars=1,resizable=1,toolbar=0,menubar=0")

        var html = "<html><head><title>SPAM - Summary</title><link rel='stylesheet' type='text/css' href='print.css'/></head><body>"


        /***************************************/
        // Overview
        /***************************************/

        html += PrintView.section("Current courses");

        html += "<ul>";
        
        for (c in Globals.courses) {
            if (Globals.courses[c].grade == 0)
                html += "<li>"+Globals.courses[c].name+"</li>";
        }
        
        html+="</ul>";
        
        
        /***************************************/
        // Exams
        /***************************************/
        
        exams = ""
        for (mod in Globals.modules) {
            if (Globals.modules[mod].moduleExam) {
                exams += "<li>"+Globals.modules[mod].name;
                if (Globals.modules[mod].oralExam) 
                    exams += " (oral)";
                else
                    exams += " (certificates)";
                exams += ": <span class='grade'>"+grade2str(Globals.modules[mod].grade)+"</span></li>";
            }
        }
        
        if (exams != "") {
            html += PrintView.section()
            html += PrintView.section("Module Exams");
            html += "<ul>"+exams+"</ul>";
        }
        
        /***************************************/
        // Modules
        /***************************************/
        
        
        html += PrintView.section()
        html += PrintView.section("Courses by Modules");
        
        for (mod in Globals.modules) {
            if (mod !="trash") {
                html += "<br /><h3>"+Globals.modules[mod].name+"</h3>";
                html += PrintView.hr()
                html += "<b>Compulsory Credits: </b>"+Globals.modules[mod].compCredits+" / "+Globals.modules[mod].maxCompCredits+"<br />"
                html += "<b>Optional Compulsory Credits: </b>"+Globals.modules[mod].optCredits+" / "+Globals.modules[mod].maxOptCredits+"<br />"
                
                if (Globals.modules[mod].moduleExam) html += "<b>Grade (module exam)</b>: "+Globals.modules[mod].grade
                else html += "<b>Mean grade</b>: "+Globals.modules[mod].grade
                html +="<ul>"
                for (c in Globals.courses) {
                    if (Globals.courses[c].module == mod) {
                        html += "<li>"+Globals.courses[c].name;
                        html += " \t<span class='grade'><span class='credits'>("+Globals.courses[c].credits+" ECTS) \t</span>";
                        html += grade2str(Globals.courses[c].grade)+"</span></li>";                        
                    }
                }                
                
                html +="</ul>"
            }
        }
        
        html += PrintView.section()
        
        html += "</body></html>";


        printView.document.open()
        printView.document.write(html)
        printView.document.close()
    }
    
}